﻿using Sx.Mall.Domain;
using Sx.Mall.WebContract;
using Sx.Mall.WebContract.Dto;
using Sx.Mall.WebService.Common;
using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using Mall.Infrastructure.EnityFramework;
using Mall.Infrastructure.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sx.Mall.WebService
{
    public class MallManagerService : IMallManagerService
    {
        private readonly AppDomain app = AppDomain.CurrentDomain;

        /// <summary>
        /// 系统人员添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Add(MallManagerDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<MallManager>();
                var entity = dto.Convert<MallManagerDto, MallManager>();
                result = MallManager.Add(Repository, entity);
                ctx.Commit();
            }
            return result;
        }

        /// <summary>
        /// 修改系统人员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Update(MallManagerDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<MallManager>();
                var entity = dto.Convert<MallManagerDto, MallManager>();
                result = MallManager.Update(Repository, entity);
                ctx.Commit();
            }
            return result;
        }

        /// <summary>
        /// 删除系统人员
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public CommonResult DeleteUser(List<int> ids)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var userRepository = ctx.GetRepository<MallManager>();
                result = MallManager.DeleteUser(userRepository, ids);
                ctx.Commit();
            }
            return result;
        }



        /// <summary>
        /// 查询系统人员
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public PageResult<MallManagerDto> GetList(MallManagerDto dto)
        {
            Page page = dto.Page;
            PageResult<MallManagerDto> result = new PageResult<MallManagerDto>();
            Expression<Func<MallManager, bool>> find = m => true;

            if (!string.IsNullOrWhiteSpace(dto.UserName))
            {
                //根据用户名模糊查找
                find = find.And(m => m.UserName.Contains(dto.UserName));
            }

            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var userRepository = ctx.GetRepository<MallManager>();

                var item = (from a in userRepository.Where(find)
                            select new MallManagerDto
                            {
                                UserID = a.UserID,
                                UserName = a.UserName,
                                RealName = a.RealName,
                                pwd = a.pwd,
                                Status = a.Status,

                            });
               
                result.rows = item.OrderBy(m => m.UserID).Paging(page).ToList();
                result.total = page.Count;
            }
            return result;
        }

    }
}
